import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-05-03
 * Time: 10:47
 */
class Solution1 {
    public int missingNumber(int[] nums) {
        Arrays.sort(nums);
        for(int i =0;i<=nums.length;++i) {
            if(i!=nums.length&&nums[i]!=i) {
                return i;
            }
        }
        return nums.length;
    }
}
class Solution2 {


    public int[] deckRevealedIncreasing(int[] deck) {
        Deque<Integer> index = new LinkedList<>();
        for(int i =0;i<deck.length;++i){
            index.add(i);
        }
        Arrays.sort(deck);
        int[] ans = new int[deck.length];
        for(int x: deck) {
            ans[index.pollFirst()] = x;
            if(!index.isEmpty()) {
                index.add(index.pollFirst());
            }
        }
        return ans;
    }
    public int[] deckRevealedIncreasing1(int[] deck) {
        int N = deck.length;
        Deque<Integer> index = new LinkedList();
        for (int i = 0; i < N; ++i)
            index.add(i);

        int[] ans = new int[N];
        Arrays.sort(deck);
        for (int card: deck) {
            ans[index.pollFirst()] = card;
            if (!index.isEmpty())
                index.add(index.pollFirst());
        }
        return ans;
    }
}

class Solution3 {
    public int minSubArrayLen(int target, int[] nums) {
        //两次循环暴力求解
        int minLength = Integer.MAX_VALUE;
        for(int i =0;i<nums.length;++i) {
            int sum =nums[i];
            for(int j =i;j<nums.length;++j) {
                if(sum>=target) {
                    minLength = Math.min(minLength,j-i+1);
                }
                sum+=nums[j];
            }
        }
        return minLength;
    }
}


class Solution24 {
    public int minSubArrayLen(int target, int[] nums) {
        int minLength = Integer.MAX_VALUE;
        int i =0;
        int sum =0;
        for(int j =0;j<nums.length;++j) {
            sum+=nums[j];
            while(sum>=target) {
                //记录窗口长度,长度应该取最小值
                minLength = Math.min(minLength,j-i+1);
                sum-=nums[i++];
            }
        }
        return minLength==Integer.MAX_VALUE?0:minLength;
    }
}
class Solution {
    public int pivotIndex(int[] nums) {
        int sum =0;
        for(int i =0;i<nums.length;++i) {
            sum+=nums[i];
        }
        int leftSum =0;
        for(int j=0;j<nums.length;++j) {
            leftSum+=nums[j];
            if(leftSum==sum-leftSum+nums[j]) {
                return j;
            }
        }
        return -1;
    }
}

public class TestDemo {

    public static void main(String[] args) {
       Solution solution = new Solution();
       int[] array = new int[] {1, 7, 3, 6, 5, 6};
       solution.pivotIndex(array);
    }

}
